package cn.itsource.basic.util.job;

import cn.itsource.order.domain.OrderAdopt;
import cn.itsource.order.mapper.OrderAdoptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
* 定时任务执行类
* @author wangcp
* @date 2021/05/10 10:42
**/
@Service
public class TaskServiceImpl implements TaskService {
    @Autowired
    private OrderAdoptMapper adoptMapper;
    @Async("taskExecutor")
    @Override
    public void executeRemindMessage() throws InterruptedException {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("当前运行线的程名称：" + Thread.currentThread().getName() + " ,执行定时任务了,执行时间为：" + sdf.format(date));
        Thread.sleep(1500);
    }

    @Override
    @Async("taskExecutor")
    public void timeOutJob() {
        String minuteAgoTime = getMinuteAgoTime();
        List<OrderAdopt> orderAdopts = adoptMapper.timeOutJob(minuteAgoTime);
        for (OrderAdopt orderAdopt : orderAdopts) {
            orderAdopt.setState(2);
            adoptMapper.updateByPrimaryKeySelective(orderAdopt);
        }

    }
    public static  String  getMinuteAgoTime () {
        String minute =  "" ;
        Date date = new Date();//获取当前时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, -30);// 30分钟前
        //获取到完整的时间
        minute =  new  SimpleDateFormat( "yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
        return  minute;
    }

    public static void main(String[] args) {
        System.out.println(getMinuteAgoTime());
    }
}